<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\UserShenhePass;
use App\UserShenhe ;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class UserShenheController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '推广审核列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new UserShenhe);
        $grid->model()->where('role_id',2)->orderBy('id','desc');
        $grid->column('id', __('Id'));


        $grid->column('type', __('类型'))->using( UserShenhe::$typeMap, '未知')->dot([
            0 => 'success',
            1 => 'danger',
            2 => 'info',
        ], 'warning');
        $grid->column('status', __('状态'))->using( UserShenhe::$statusMap, '未知')->dot([
           1 => 'success',
            3 => 'danger',
            2 => 'info',
            0=>'warning'
        ], 'warning');
        $grid->column('adminWorkshop.id', __('盘源'))->display(function ($value){
            return "<a href='/admin/workshop/$value/edit' target='_blank'>点击查看 </a>";
        });
        $grid->column('adminWorkshop.title','盘源标题');
        $grid->actions(function (Grid\Displayers\Actions $actions) {

            $actions->disableDelete();
            // 去掉编辑
            $actions->disableEdit();
            $actions->disableView();
            $actions->getValue();
            if ($actions->row->status==UserShenhe::STATUS_SHENHE){

                $actions->add(new \App\Admin\Actions\UserShenhePass());
                $actions->add(new \App\Admin\Actions\UserShenheRefuse());
            }



        });
        $grid->column('code', __('盘源编号'));
        $grid->column('user.name', __('申请人'));
        $grid->column('created_at', __('申请时间'));
        $grid->column('refuseReason', __('拒绝原因'));
        $grid->filter(function (Grid\Filter $filter){
            $filter->like('code','编码');
            $filter->like('user.name','申请人');
            $filter->disableIdFilter();
            $filter->in('type', '类型')->multipleSelect( UserShenhe::$typeMap);
            $filter->in('status', '状态')->multipleSelect( UserShenhe::$statusMap);
            $filter->like('workshop.title','盘源标题');
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(UserShenhe::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('title', __('Title'));
        $show->field('desc', __('Desc'));
        $show->field('type', __('Type'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('workshop_id', __('Workshop id'));
        $show->field('role_id', __('Role id'));
        $show->field('is_read', __('Is read'));
        $show->field('code', __('Code'));
        $show->field('user_id', __('User id'));
        $show->field('status', __('Status'));
        $show->field('refuseReason', __('RefuseReason'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new UserShenhe);

        $form->text('title', __('Title'));
        $form->text('desc', __('Desc'));
        $form->number('type', __('Type'));
        $form->number('workshop_id', __('Workshop id'));
        $form->number('role_id', __('Role id'));
        $form->number('is_read', __('Is read'));
        $form->text('code', __('Code'));
        $form->number('user_id', __('User id'));
        $form->text('status', __('Status'));
        $form->text('refuseReason', __('RefuseReason'));

        return $form;
    }
}
